package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"operator/pkg/tracing"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/test2", func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("test2")
		fmt.Fprint(w, "222222222222")
	})
	mux.HandleFunc("/test1", func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("test1")
		fmt.Fprint(w, "111111111111")
	})
	mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("test")
		fmt.Fprint(w, "000000000000")
	})
	mux.HandleFunc("/testbaidu", func(w http.ResponseWriter, r *http.Request) {
		res, _ := http.Get("https://www.baidu.com")
		data, _ := ioutil.ReadAll(res.Body)
		fmt.Fprint(w, data)
	})
	mux.HandleFunc("/testpost", func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("testpost")
		res, err := tracing.NewRequestWithTrace(r, "GET", "http://192.168.189.128:8999/test3", nil)
		if err != nil {
			fmt.Println(err)
			fmt.Fprint(w, err.Error())
			return
		}

		c := http.Client{}
		ress, err := c.Do(res)
		if err != nil {
			fmt.Println(err)
			fmt.Fprint(w, "not found")
			return
		}
		if ress.Body != nil {
			d, _ := ioutil.ReadAll(ress.Body)
			fmt.Fprint(w, string(d))
			return
		}
		fmt.Fprint(w, "not found")
	})

	http.ListenAndServe(":8555", mux)
}
